package com.rd.student.ruanjian.class4.lichunquan.L_2023_7_11;

import java.util.ArrayList;
import java.util.HashMap;

public class Test01 {
    public static void main(String[] args) {
        HashMap_Test hashMap_test = new HashMap_Test();
        hashMap_test.test02();
    }
}
class HashMap_Test {
    HashMap hashMap = new HashMap();
    public void test02() {
        //添加数据
        int sum;
        HashMap hashMap = new HashMap();
        Student s1 = new Student(0, "李春泉", "软件", "开心", 20, 4);
        Student s2 = new Student(1, "周子贺", "软件", "开心", 20, 4);
        Student s3 = new Student(2, "高艺铭", "软件", "开心", 20, 4);
        Student s4 = new Student(3, "王星凯", "软件", "开心", 20, 4);
        Student s7 = new Student(4, "柏云耀", "软件", "开心", 20, 4);
        Student s5 = new Student(5, "杜航", "软件", "开心", 20, 5);
        Student s6 = new Student(6, "张睿", "软件", "开心", 20, 4);
        hashMap.put(s1.getId(), s1);
        hashMap.put(s2.getId(), s2);
        hashMap.put(s3.getId(), s3);
        hashMap.put(s4.getId(), s4);
        hashMap.put(s7.getId(), s7);
        hashMap.put(s5.getId(), s5);
        hashMap.put(s6.getId(), s6);
        //以班级为key
        System.out.println(getHashMap(hashMap, 4));
        //以专业为key
        System.out.println(getNumOfZhuanYe(hashMap, "软件"));
        //以年龄为key
        System.out.println(getNumOfAge(hashMap, 20));
    }

    //以班级为key
    public HashMap getHashMap(HashMap hashMap, int cls) {
        HashMap hashMap1 = new HashMap();
        Student s;
        int sum = 0;
        for (int i = 0; i < hashMap.size(); i++) {
            s = (Student) hashMap.get(i);
            if (s.getClass1() == cls) {
                sum++;
                hashMap1.put(s.getClass1(), sum);
            }
        }
        return hashMap1;
    }

    //以专业为key
    public HashMap getNumOfZhuanYe(HashMap hashMap, String zhuanye) {
        HashMap hashMap1 = new HashMap();
        Student s;
        int sum = 0;
        for (int i = 0; i < hashMap.size(); i++) {
            s = (Student) hashMap.get(i);
            if (s.getZhuanye().equals(zhuanye)) {
                sum++;
                hashMap1.put(s.getZhuanye(), sum);
            }
        }
        return hashMap1;
    }

    //以年龄为key
    public HashMap getNumOfAge(HashMap hashMap, int age) {
        HashMap hashMap1 = new HashMap();
        Student s;
        int sum = 0;
        for (int i = 0; i < hashMap.size(); i++) {
            s = (Student) hashMap.get(i);
            if (s.getAge() == age) {
                sum++;
                hashMap1.put(s.getAge(), sum);
            }
        }
        return hashMap1;
    }

    class Student {
        int id;
        String name;
        String zhuanye;
        String miaoshu;
        Integer age;
        Integer class1;
        ArrayList arrayList;
        ArrayList arrayListname = new ArrayList();
        ArrayList arrayListAge = new ArrayList();

        Student() {
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Student(int id, String name, String zhuanye, String miaoshu, Integer age, Integer class1) {
            this.id = id;
            this.name = name;
            this.zhuanye = zhuanye;
            this.miaoshu = miaoshu;
            this.age = age;
            this.class1 = class1;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getZhuanye() {
            return zhuanye;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public void setZhuanye(String zhuanye) {
            this.zhuanye = zhuanye;
        }

        public int getClass1() {
            return class1;
        }

        public void setClass1(int class1) {
            this.class1 = class1;
        }

        @Override
        public String toString() {
            return "学生：" +
                    "姓名：" + name + '\'' +
                    ", 专业：" + zhuanye + '\'' +
                    ", 班级：" + class1;
        }
    }
}
